using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using BusinessLayer;
using EntityLayer;
using InfraStructureLayer;

public partial class Photos_Default : SFPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected string GeneratePhotoUrl(object obj)
    {
        Crypto c = new Crypto();
        return "~/Handlers/Image.ashx?Url=" + c.EncryptURL(obj.ToString()) + "&Width=120";
    }
    protected string GeneratePhotoUrlPreview(object obj)
    {
        Crypto c = new Crypto();
        return "~/Handlers/Image.ashx?Url=" + c.EncryptURL(obj.ToString()) + "&Width=550";
    }
    protected string GenerateAlbumUrl(object obj)
    {
        Crypto c = new Crypto();
        return "~/" + CurrentContext.Language + "/photos/Album" + c.EncryptURL(obj.ToString()) + ".aspx";
    }
    protected bool sflPhotos_NeedsDataSource()
    {
        if (ViewState["PageNumber"] == null)
        {
            ViewState["PageNumber"] = 1;
        }
        busPhotos insBusPhotos = new busPhotos();
        DataTable insDataTablePhotos = null;

        if (this.QueryStringPhoto.ToString() == "")
        {
            if (Request.QueryString["Photo"] != null && Request.QueryString["Photo"] != "")
            {
                this.QueryStringPhoto = Request.QueryString["Photo"];
            }
        }
        if (this.QueryStringAlbum.ToString() == "")
        {
            if (Request.QueryString["Album"] != null && Request.QueryString["Album"] != "")
            {
                this.QueryStringAlbum = Request.QueryString["Album"];
            }
        }

        if (QueryStringAlbum == "" && QueryStringPhoto == "")
        {
            insDataTablePhotos = insBusPhotos.SelectPhotosByLanguageId();
        }
        else if (QueryStringPhoto == "")
        {
            entPhotos insEntPhotos = new entPhotos();
            string album = new Crypto().DecryptURL(QueryStringAlbum);

            insEntPhotos.PhotoAlbumId = Convert.ToInt32(album);
            insDataTablePhotos = insBusPhotos.SelectPhotosByLanguageIdAndPhotoAlbumId(insEntPhotos);
        }
        else
        {
            entPhotos insEntPhotos = new entPhotos();
            string photoId = new Crypto().DecryptURL(QueryStringPhoto);

            insEntPhotos.PhotoId = Convert.ToInt32(photoId);
            insDataTablePhotos = insBusPhotos.SelectPhotosByLanguageIdAndPhotoId(insEntPhotos);
        }
        if (insBusPhotos.HasErrors)
        {
            MessageBoxShow(insBusPhotos);
            return true;
        }

        DataTable insDataTablePhotosClone = insDataTablePhotos.Clone();
        int start = (Convert.ToInt32(ViewState["PageNumber"]) - 1) * 16;
        int end = Convert.ToInt32(ViewState["PageNumber"]) * 16;
        for (int i = start; i < end; i++)
        {
            if (i < insDataTablePhotos.Rows.Count)
            {
                insDataTablePhotosClone.ImportRow(insDataTablePhotos.Rows[i]);
            }
        }

        sflPhotos.SetData(insDataTablePhotosClone);
        Session["DataTablePhotos"] = insDataTablePhotos;

        DataTable dtPages = new DataTable();
        dtPages.Columns.Add("PageNumber", typeof(int));
        int j = 0;
        for (int i = insDataTablePhotos.Rows.Count; i > 0; i = i - 16)
        {
            j++;
            dtPages.Rows.Add(new object[] { j });
        }

        rptPages.DataSource = dtPages;
        rptPages.DataBind();

        return insBusPhotos.HasErrors;
    }
    protected void sflPhotos_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Preview")
        {
            
        }
    }

    protected void rptPages_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "ChangePage")
        {
            ViewState["PageNumber"] = e.CommandArgument;
            sflPhotos.LoadDataListData();
        }
    }
    protected bool GetEnabled(object o)
    {
        return (Convert.ToInt32(o) != Convert.ToInt32(ViewState["PageNumber"]));
    }
    protected void sflPhotos_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        ((ImageButton)e.Item.FindControl("imgPhoto")).OnClientClick = "OpenPhoto('" + new Crypto().EncryptURL(((DataRowView)e.Item.DataItem)["PhotoId"].ToString()) + "');return false;";
    }
}
